// components/field/index.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    value: {
      type: String,
      value: ''
    },
    placeholder: {
      type: String,
      value: '输入'
    },
    params: {
      type: Array,
      value: []
    },
    type: {
      type: String,
      value: 'text'
    },
    size: {
      type: String,
      value: 'normal'
    },
    titleWidth: {
      type: String,
      value: 0
    },
    label: {
      type: String,
      value: ''
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    show: false,
    positionBottom: 0
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onShow() {
      wx.showModal({
        editable: true,
        content: this.properties.value,
        title: this.properties.label || '填写',
        success: (res)=>{
          if(res.confirm){
            this.triggerEvent('change', {
              value: res.content,
              params: this.properties.params
            });
          }
        }
      })
    },
    onHide() {
      this.setData({
        show: false
      })
    },
    setFieldPosition(height) {
      let res = wx.getSystemInfoSync();
      this.setData({
        positionBottom: res.platform.toLocaleLowerCase() === 'android' ? '-' + (height + 35) + 'px' : '-' + height + 'px'
      })
    },
    onChange(event) {
      this.triggerEvent('change', {
        value: event.detail,
        params: this.properties.params
      });
    },
    onFocus() {
      this.onShow();
    },
    onKeyboardheightchange(event) {
      const {
        height
      } = event.detail;
      this.setFieldPosition(height);
    },
    onConfirm() {
      this.onHide();
    },
    onBlur() {
      this.onHide();
    },
  },
  lifetimes: {
    attached: function () {
      // 在组件实例进入页面节点树时执行

 
    }
  }
})